﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BLL;
using BLL.Entities;
using BLL.Managers;
using CorporateGymManagementSystem.Binding_classes;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for InboxWindow.xaml
    /// </summary>
    public partial class InboxWindow : Window
    {
        private GymWorker loggedInWorker;
        ObservableCollection<OutboxMessageDataGridItem> dataGridOutgoingMessageList = new ObservableCollection<OutboxMessageDataGridItem>();
        ObservableCollection<InboxMessageDataGridItem> dataGridIncomingMessageList = new ObservableCollection<InboxMessageDataGridItem>();

        public InboxWindow(GymWorker i_LoggedInWorker)
        {
            InitializeComponent();
            loggedInWorker = i_LoggedInWorker;
            loadMessages();
        }

        private void loadMessages()
        {

            loadInboxMessages();
            loadOutboxMessages();
            
        }

        private void loadOutboxMessages()
        {
            try
            {
                List<Message> messageList = MessagesManager.Instance.GetWorkerOutgoingMessages(loggedInWorker.WorkerId);
                
                foreach (Message message in messageList)
                {
                    List<GymWorker> recievers = MessagesManager.Instance.GetMessageReceivers(message.MessageId);
                    StringBuilder receiversNames = new StringBuilder();
                    foreach (GymWorker receiver in recievers)
                    {
                        receiversNames.Append(receiver.FirstName).Append("; ");

                    }
                    OutboxMessageDataGridItem messageDataGridItem = new OutboxMessageDataGridItem { Message = message, Receivers = recievers, Subject = message.Subject, Content = message.Text, Date = message.CreateTime, To = receiversNames.ToString() };
                    dataGridOutgoingMessageList.Add(messageDataGridItem);
                }

                
                outboxDataGrid.DataContext = dataGridOutgoingMessageList;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.StackTrace.ToString());
            }
        }

        private void loadInboxMessages()
        {
            try
            {
                List<Message> messageList = MessagesManager.Instance.GetWorkerIncomingMessages(loggedInWorker.WorkerId);
                foreach (Message message in messageList)
                {
                    GymWorker sender = GymWorkerManager.Instance.GetWorkerById(message.SenderId);
                    if (sender != null)
                    {
                        InboxMessageDataGridItem messageDataGridItem = new InboxMessageDataGridItem { Message = message, Sender = sender, Subject = message.Subject, Content = message.Text, Date = message.CreateTime, From = sender.FirstName };
                        dataGridIncomingMessageList.Add(messageDataGridItem);
                    }
                }

                inboxDataGrid.DataContext = dataGridIncomingMessageList;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.StackTrace.ToString());
            }
        }
        

        private void ButtonBar_Loaded(object sender, RoutedEventArgs e)
        {
            ToolBar toolBar = sender as ToolBar;
            var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
            if (overflowGrid != null)
            {
                overflowGrid.Visibility = Visibility.Collapsed;
            }
        }

        private void toolBarComposeNewMessageButton_Click(object sender, RoutedEventArgs e)
        {
            ComposeNewMessageWindow composeNewMessageWindow = new ComposeNewMessageWindow(loggedInWorker, eComposeType.RegularMessage);
            composeNewMessageWindow.ShowDialog();
            dataGridOutgoingMessageList.Clear();
            loadOutboxMessages();
        }

        private void toolBarComposeTraineeMessageButton_Click(object sender, RoutedEventArgs e)
        {
            ComposeNewMessageWindow composeNewMessageWindow = new ComposeNewMessageWindow(loggedInWorker, eComposeType.TraineeMessage);
            composeNewMessageWindow.ShowDialog();
            dataGridOutgoingMessageList.Clear();
            loadOutboxMessages();
        }

        private void inboxDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender != null)
            {
                DataGridRow dgr = sender as DataGridRow;
                InboxMessageDataGridItem messageItem = (inboxDataGrid.SelectedItem as InboxMessageDataGridItem);

                ViewMessageWindow viewMessageWindow = new ViewMessageWindow(messageItem, loggedInWorker);
                viewMessageWindow.ShowDialog();
            }
        }

        private void outboxDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender != null)
            {
                DataGridRow dgr = sender as DataGridRow;
                OutboxMessageDataGridItem messageItem = (outboxDataGrid.SelectedItem as OutboxMessageDataGridItem);

                ViewMessageWindow viewMessageWindow = new ViewMessageWindow(messageItem, loggedInWorker);
                viewMessageWindow.ShowDialog();
            }
        }

        private void toolBarComposeBroadcastMessageButton_Click(object sender, RoutedEventArgs e)
        {
            ComposeNewMessageWindow composeNewMessageWindow = new ComposeNewMessageWindow(loggedInWorker, eComposeType.BroadcastMessage);
            composeNewMessageWindow.ShowDialog();
        }
    }
}
